package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;

import com.cdck.safe.dete.model.bo.SeatBO;
import com.cdck.safe.dete.model.entity.Seat;

import com.cdck.safe.dete.model.query.SeatQuery;
import com.cdck.safe.dete.model.vo.SeatDataVo;
import com.cdck.safe.dete.model.vo.SeatStatusVO;
import com.cdck.safe.dete.service.SeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * (Seat)表控制层
 *
 * @author wp
 * @since 2024-02-25 16:47:16
 */
@Api(value = "seat")
@RestController
@RequestMapping("seat")
@RequiredArgsConstructor
public class SeatController {

    private final SeatService seatService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams", value = "查询条件")
    })
    public PageResult<SeatDataVo> queryByPage(SeatQuery queryParams) {
        IPage<SeatDataVo> result = seatService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<Seat> getSeatForm(
            @PathVariable Long id
    ) {
        Seat formData = seatService.getSeatFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param seat 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
            @RequestBody @Valid
                    Seat seat
    ) {
        boolean result = this.seatService.saveSeat(seat);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateSeat(
            @PathVariable Long id,
            @RequestBody @Validated Seat seat) {
        boolean result = seatService.updateSeat(id, seat);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteSeats(
            @PathVariable String ids
    ) {
        boolean result = seatService.deleteSeats(ids);
        return Result.judge(result);
    }

    @ApiOperation(value = "下拉选择数据")
    @GetMapping("/list/{labId}")
    public Result list(@PathVariable Long labId) {
        LambdaQueryWrapper<Seat> seatWrapper = new LambdaQueryWrapper<Seat>();
        seatWrapper.eq(Seat::getLabId, labId);
        seatWrapper.eq(Seat::getStatus, 0);
        List<Seat> list = seatService.list(seatWrapper);
        return Result.success(list);
    }

    @ApiOperation(value = "根据实验室id查询座位列表")
    @GetMapping("/client/{id}")
    public Result<List<Seat>> getSeatListByLabId(
            @PathVariable Long id
    ) {
        List<Seat> result = seatService.getSeatListByLabId(id);
        return Result.success(result);
    }

    @ApiOperation(value = "获取实验室座位状态")
    @PostMapping("/status")
    public Result<List<SeatStatusVO>> getSeatListByLabId(@RequestBody SeatBO seatBO) {
        List<SeatStatusVO> result = seatService.getSeatStatus(seatBO);
        return Result.success(result);
    }

}

